//
//  AnswerViewController.m
//  StudyDrive
//
//  Created by 蔡欣东 on 15/8/26.
//  Copyright (c) 2015年 蔡欣东. All rights reserved.
//

#import "AnswerViewController.h"
#import "AnswerScollView.h"
#import "MyDataManager.h"
#import "AnswerModel.h"
#import "SelectModelView.h"
#import "SheetView.h"
@interface AnswerViewController ()<SheetViewDelegate>{
    AnswerScollView* _answerScrollView;
    SelectModelView* modelView;
    SheetView* sheetView;
    NSMutableArray* arr;
}

@end

@implementation AnswerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    arr = [[NSMutableArray alloc]init];
    NSArray* array = [MyDataManager getData:answer];
    for(int i=0;i<array.count-1;i++){
        AnswerModel* model = array[i];
        if ([model.pid intValue]==_number+1) {//选择章节
            [arr addObject:model];
        }
    }
    _answerScrollView = [[AnswerScollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64-60) withDataArray:arr];
    self.title = @"章节练习";
    [self.view addSubview:_answerScrollView];
    [self createToolBar];
    [self createModelView];
    [self createSheetView];
}
-(void)createSheetView{
    sheetView = [[SheetView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-80) withSuperView:self.view andQuesCount:arr.count];
    sheetView.delegate = self;
    [self.view addSubview:sheetView];

}
#pragma mark - delegate
-(void)SheetViewButtonClick:(int)index{
    UIScrollView* scroll = _answerScrollView->_scrollView;
    scroll.contentOffset = CGPointMake((index-1)*scroll.frame.size.width, 0);
    [scroll.delegate scrollViewDidEndDecelerating:scroll];
}
-(void)createModelView{
    modelView = [[SelectModelView alloc]initWithFrame:self.view.frame addTouch:^(SelectModel model) {
        NSLog(@"当前模式：%d",model);
    }];
    [self.view addSubview:modelView];
    modelView.alpha = 0;
    UIBarButtonItem* item = [[UIBarButtonItem alloc]initWithTitle:@"答题模式" style:UIBarButtonItemStylePlain target:self action:@selector(modelChange:)];
    item.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = item;
}
-(void)modelChange:(UIBarButtonItem*)item{
    [UIView animateWithDuration:0.3 animations:^{
        modelView.alpha = 1;
    }];
}
-(void)createToolBar{
    UIView* barView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-60-64, self.view.frame.size.width, 60)];
    barView.backgroundColor = [UIColor whiteColor];
    NSArray* arr1 = @[@"",@"查看答案",@"收藏本题"];
    for (int i=0; i<3; i++) {
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(self.view.frame.size.width/3*i+self.view.frame.size.width/3/2-22,5, 30, 30);
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",16+i]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d-2.png",16+i]] forState:UIControlStateHighlighted];
        btn.tag = 301+i;
        [btn addTarget:self action:@selector(clickToolBar:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(btn.center.x-30, 40, 60, 18)];
        label.textAlignment = NSTextAlignmentCenter;
        if (i==0) {
            label.text = [NSString stringWithFormat:@"1/%lu",(unsigned long)arr.count];
        }else{
        label.text = arr1[i];
        }
        label.textColor = [UIColor grayColor];
        label.font = [UIFont systemFontOfSize:12];
        [barView addSubview:btn];
        [barView addSubview:label];
    }
    [self.view addSubview:barView];
}
-(void)clickToolBar:(UIButton*)btn{
    switch (btn.tag) {
        case 301:{
            [UIView animateWithDuration:0.3 animations:^{
                sheetView.frame = CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height-80);
                sheetView->_backView.alpha = 0.8;
            }];
        }
            break;
        case 302:{//查看答案
            if ([_answerScrollView.hadAnswerArray[_answerScrollView.currentPage]integerValue]!=0) {
                return;
            }else{
                AnswerModel* model = [_answerScrollView.dataArray objectAtIndex:_answerScrollView.currentPage];
                NSString* answer = model.manswer;
                char an = [answer characterAtIndex:0];
                [_answerScrollView.hadAnswerArray replaceObjectAtIndex:_answerScrollView.currentPage withObject:[NSString stringWithFormat:@"%d",an-'A'+1]];//填写进去正确答案
                _answerScrollView.reloadData;
                NSLog(@"give answer");
            }
        }
            break;
            
        default:
            break;
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
